
function createList(pageNo = 1, pageSize = 10) {


    var matchData = {};
    var start = (pageNo - 1) * pageSize;
    // 请求数据
    apiAdminGetRecord('rechargeModel', start, pageSize, matchData,
        function (data) {
            setTimeout(() => {
                data.msg.recordArr.sort((a, b) => { return a.count - b.count });
                $('#dataList').bootstrapTable('load', data.msg);
            }, 200)
        });
}


function addNew() {

    $('#addNew').modal('show');
}

function saveAdmin() {

    var count = $("#count").val();
    if (!count || count === 0) {
        Swal.fire('请输入金额');
        return;
    }
    var gold = $("#gold").val();
    if (!gold || gold === 0) {
        Swal.fire('请输入钻石');
        return;
    }
    var goodID = $("#goodID").val();
    if (!goodID || goodID === 0) {
        Swal.fire('请输入商品唯一ID');
        return;
    }

    var addGold = $("#addGold").val() || 0;

    addRecharge(count, gold, addGold, goodID);

    $('#addNew').modal('hide');
}

function deletData(count) {

    apiAdminDeleteRecord('rechargeModel', { 'count': count }, function () {
        $.modal({ body: '删除成功' });
        createList();
    });
}

function addRecharge(count, gold, addGold, goodID, cb) {


    let saveData = {
        count: count,
        gold: gold,
        addGold: addGold,
        goodID: goodID,
    };
    apiAdminAddRecord('rechargeModel', saveData, function (data) {
        if (data.code === 0) {
            $("#count").val('');
            $("#gold").val('');
            $("#goodID").val('');
            $("#addGold").val('');
            $.modal({ body: '新增成功' });
            createList();
        }
    })

}



$(document).ready(function () {

    //数据表格初始化
    $('#dataList').bootstrapTable({
        striped: true, // 是否显示行间隔色
        pageNumber: 1, // 初始化加载第一页
        pagination: true, // 是否分页
        theadClasses: "thead-light",//这里设置表头样式
        sidePagination: 'server', // server:服务器端分页|client：前端分页
        pageSize: 10, // 单页记录数
        pageList: [10, 20, 30],
        totalField: "totalCount",
        dataField: "recordArr",
        buttonsClass: 'light',
        buttonsPrefix: 'btn',
        showRefresh: false,// 刷新按钮
        onPageChange: function (number, size) {
            createList(number, size);
        },
        queryParams: function (params) { // 上传服务器的参数
            let uid = $("#txtSearch").val();
            var temp = {//如果是在服务器端实现分页，limit、offset这两个参数是必须的
                limit: params.limit, // 每页显示数量
                offset: params.offset, // SQL语句起始索引
                uid: uid,
            };
            return temp;
        },
        columns: [
            {
                field: 'goodID',
                title: '商品ID',
                //width: 150,
                align: "center",
                valign: "middle",
            }, {
                field: 'count',
                title: '金额',
                //width: 150,
                align: "center",
                valign: "middle",
            },
            {
                field: 'gold',
                title: '钻石',
                //width: 300,
                align: "center",
                valign: "middle",
            },
            {
                field: 'addGold',
                title: '赠送数量',
                // width: 300,
                align: "center",
                valign: "middle",
            },
            {
                title: '操作',
                width: 200,
                align: "center",
                valign: "middle",
                formatter: option
            }]
    });
    createList();

    // 定义删除、更新按钮
    function option(value, row, index) {
        var htm = `<button type="button" class="btn btn-light"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="删除配置" onClick="deletData('${row.count}',true)"><i class="bi bi-trash3-fill fs-5 text-danger"></i></button>`;
        return htm;
    }



});